from django.http import Http404,HttpResponseRedirect
from django.shortcuts import render_to_response
from django import forms
from djangocollection.djcollection.views import GenericCollection

class SubtaskCollection(GenericCollection):
    def __call__(self, request, **kw):
        self.taskid = request.GET.get('task', '')
        return super(SubtaskCollection, self).__call__(request, 'task', 'subtask', **kw)

    def list(self):
        if not self.taskid:
            raise Http404
        return HttpResponseRedirect('/task/%s/'%self.taskid)

    def delete(self):
        self.model.objects.filter(id=self.id).delete()
        return HttpResponseRedirect('../../task/%s'%self.request.POST.get('task'))

    def get_create_form(self):
        if not self.taskid:
            raise Http404
        form = forms.FormWrapper(self.model.AddManipulator(), {}, {})
        template_name = '%s/%s_form.html' % ( self.template_dir, self.model_name )
        return render_to_response(template_name, {'form':form, 'task_id':self.taskid})
